import random
from datetime import timedelta, datetime
from config.user import TIME_SPAN
# 注意未采用时区分区

#根据时间戳分析属于哪个时段
def deal_time_span(time):
    ranges = {
        '0~4': range(0, 5),
        '5~9': range(5, 10),
        '10~14': range(10, 15),
        '15~19': range(15, 20),
        '20~24': range(20, 25)
    }

    for range_span, rng in ranges.items():
        if int(time) in rng:
            return TIME_SPAN.get(range_span)


# 计算当前周的周一和周日时间戳
def deal_current_weekday_start_end():
    now = datetime.now()
    start_of_week = now - timedelta(days=now.weekday())  # 周一
    end_of_week = start_of_week + timedelta(days=6)  # 周日
    start_timestamp = int(start_of_week.timestamp())
    end_timestamp = int(end_of_week.timestamp()) + 86399  # 加上一天的秒数减一（为了包含当天的最后一秒）
    week_timestamps = {
        'mon': int((start_of_week).timestamp()),
        'tue': int((start_of_week + timedelta(days=1)).timestamp()),
        'wed': int((start_of_week + timedelta(days=2)).timestamp()),
        'thu': int((start_of_week + timedelta(days=3)).timestamp()),
        'fri': int((start_of_week + timedelta(days=4)).timestamp()),
        'sat': int((start_of_week + timedelta(days=5)).timestamp()),
        'sun': int((start_of_week + timedelta(days=6)).timestamp())
    }

    return (start_timestamp, end_timestamp),week_timestamps


# 根据时间戳是周数
def get_week_number(timestamp):
    date = datetime.fromtimestamp(timestamp)
    start_of_week = date - timedelta(days=date.weekday())
    week_number = start_of_week.strftime('%V')
    return week_number


# 判断是是否为同一周
def are_in_same_week(timestamp1, timestamp2):
    week_number1 = get_week_number(timestamp1)
    week_number2 = get_week_number(timestamp2)
    return week_number1 == week_number2


# 根据指定时间戳返回上一周的周一和周日时间戳
def get_previous_week_start_end(timestamp):
    # 根据时间戳获取是星期几（0是周一，6是周日）
    weekday = timestamp.weekday()
    # 计算上一周周一的日期（今天减去相应的天数回到上一周的周一）
    previous_week_start = timestamp - timedelta(days=weekday) - timedelta(days=7)

    # 计算上一周周日的日期（周一的基础上加6天）
    previous_week_end = previous_week_start + timedelta(days=6)

    # 转换为时间戳
    previous_week_start_timestamp = int(previous_week_start.timestamp())
    previous_week_end_timestamp = int(previous_week_end.timestamp())

    return previous_week_start_timestamp, previous_week_end_timestamp


def get_str_date_uniform():
    pre=str(random.uniform(0, 1))[2:6]
    timestamp=str(int(datetime.now().timestamp()))
    return pre+timestamp





